<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
//用户社区账户表
class Bbs_account_model extends CI_Model{
    function __construct() {
        parent::__construct();
        $this->table = 'bbs_account';
        $this->users_info = 'users_info';
        $this->bbs_fans = 'bbs_fans';
        $this->bbs_article = 'bbs_article';
        $this->bbs_favorite = 'bbs_favorite';
        $this->bbs_article_comment = 'bbs_article_comment';
        $this->bbs_article_category = 'bbs_article_category';
        $this->bbs_reward = 'bbs_reward';
        $this->bbs_friends = 'bbs_friends';
        $this->bbs_count = 'bbs_count';
        $this->bbs_user = 'bbs_user';
        $this->bbs_letter = 'bbs_letter';
        $this->bbs_user_degree = 'bbs_user_degree';
        $this->bbs_exp_rule = 'bbs_exp_rule';
        $this->bbs_invite = 'bbs_invite';
        $this->bbs_focus = 'bbs_focus';
        $this->bbs_exp_list = 'bbs_exp_list';
        $this->bbs_notice = 'bbs_notice';
        $this->bbs_user_behavior = 'bbs_user_behavior';
        $this->bbs_zhu_detail = 'bbs_zhu_detail';
    }
    function get($uid){
        return $this->db->select('*')->from($this->table)->where('user_id',$uid)->get()->row_array();
    }
    
    function get_user_count($user_id){
        return $this->db->select('*')->from($this->bbs_count)->where('user_id',$user_id)->get()->row_array();
    }
    
    //获取用户的粉丝总数
    function get_user_fans_count($uid){
        return $this->db->select('*')->from($this->bbs_fans)->where('user_id',$uid)->get()->num_rows();
    }
    
    //获取用户的粉丝
    function get_user_fans_list($uid,$page,$limit){
        $this->db->select($this->bbs_fans.'.*,'.$this->users_info.'.head_img,'.$this->users_info.'.nickname');
        $this->db->from($this->bbs_fans);
        $this->db->join($this->users_info,$this->users_info.'.user_id='.$this->bbs_fans.'.followuid',left);
        $this->db->where($this->bbs_fans.'.user_id',$uid);
        $this->db->limit($limit,$page);
        return $this->db->get()->result_array();
    }
    
    //获取他关注的板块
    function get_user_category_focus($where,$page,$limit){
        $this->db->select($this->bbs_focus.'.*,'.$this->bbs_article_category.'.name,'.$this->bbs_article_category.".group,".$this->bbs_article_category.".code,".$this->bbs_article_category.".icon");
        $this->db->from($this->bbs_focus);
        $this->db->join($this->bbs_article_category,$this->bbs_article_category.'.id='.$this->bbs_focus.'.cat_id and tan_bbs_article_category.is_show = 0',left);
        $this->db->where($where);
        $this->db->order_by($this->bbs_focus.'.create_time','desc');
        $this->db->limit($limit,$page);
        return $this->db->get()->result_array();
    }
    function readnum($user_id){
        return $this->db->select_sum('readnum')->from($this->bbs_article)->where('authorid',$user_id)->get()->row_array();
    }
    function fanscount($uid){
         return $this->db->select('*')->from($this->bbs_fans)->where('user_id',$uid)->get()->num_rows();

    }
    
    //获取社区用户个人收藏文章总数
    function get_article_user_favorite_count($where){
        
    }
    
    //获取社区用户个人收藏文章列表
    function get_article_user_favorite_list($where,$page,$limit){
        $this->db->select($this->bbs_favorite.'.*,'.
                $this->bbs_article.".title,".
                $this->bbs_article.".likes,".
                $this->bbs_article.".cat_id,".
                $this->bbs_article.".summary,".
                $this->bbs_article.".thumb,".
                $this->bbs_article.".authorid,".
                $this->bbs_article.".likes,".
                $this->bbs_article.".commentnum");
        $this->db->from($this->bbs_favorite);
        $this->db->join($this->bbs_article,$this->bbs_article.'.id='.$this->bbs_favorite.'.article_id',"left");
        $this->db->where($where);
        $this->db->order_by($this->bbs_favorite.".create_time DESC");
        $this->db->limit($limit,$page);
        return $this->db->get()->result_array();
    }
    
    //获取打赏记录总数
    function get_article_user_award_count($where){
        return $this->db->select('*')->from($this->bbs_reward)->where($where)->get()->num_rows();
    }
    //获取打赏记录列表
    function get_article_user_award_list($where,$page,$limit){
        $this->db->select($this->bbs_reward.'.*,'.
                $this->bbs_article.".title,".$this->bbs_article.".cat_id,u1.nickname user_nickname,u1.head_img user_head_img,u2.nickname author_nickname,u2.head_img author_head_img");
        $this->db->from($this->bbs_reward);
        $this->db->join($this->bbs_article,$this->bbs_article.'.id='.$this->bbs_reward.'.article_id',"left");
        $this->db->join($this->users_info." u1",'u1.user_id='.$this->bbs_reward.'.user_id',"left");
        $this->db->join($this->users_info." u2",'u2.user_id='.$this->bbs_reward.'.authorid',"left");
        $this->db->where($where);
        $this->db->order_by($this->bbs_reward.".create_time DESC");
        $this->db->limit($limit,$page);
        return $this->db->get()->result_array();
    }
    //获取用户的粉丝总数
    function get_account_user_fans_count($where){
        $this->db->select($this->bbs_fans.'.*');
        $this->db->from($this->bbs_fans);
        $this->db->where($where);
        $this->db->order_by($this->bbs_fans.".create_time DESC");
        return $this->db->get()->num_rows();
    }
    
    //获取用户的粉丝列表
    function get_account_user_fans_list($where,$page,$limit){
        $this->db->select($this->bbs_fans.'.*,'.
                $this->users_info.'.head_img,'.
                $this->users_info.'.sex,'.
                $this->users_info.'.nickname,'.
                $this->bbs_user.'.salt,'.
                $this->bbs_count.'.fans_count,'.
                $this->bbs_count.'.articles_count,'.
                $this->bbs_count.'.question_count');
        $this->db->from($this->bbs_fans);
        $this->db->join($this->users_info,$this->users_info.'.user_id='.$this->bbs_fans.'.followuid',left);
        $this->db->join($this->bbs_count,$this->bbs_count.'.user_id='.$this->bbs_fans.'.followuid',left);
        $this->db->join($this->bbs_user,$this->bbs_user.'.user_id='.$this->bbs_fans.'.followuid',left);
        $this->db->where($where);
        $this->db->order_by($this->bbs_fans.".create_time DESC");
        $this->db->limit($limit,$page);
        return $this->db->get()->result_array();
    }
    //获取用户的粉丝总数
    function get_account_user_focus_count($where){
        $this->db->select($this->bbs_fans.'.*');
        $this->db->from($this->bbs_fans);
        $this->db->where($where);
        $this->db->order_by($this->bbs_fans.".create_time DESC");
        return $this->db->get()->num_rows();
    }
    //获取个人中心用户的关注
    function get_account_user_focus_list($where,$page,$limit){
        $this->db->select($this->bbs_fans.'.*,'.
                $this->users_info.'.head_img,'.
                $this->users_info.'.sex,'.
                $this->users_info.'.nickname,'.
                $this->bbs_user.'.salt,'.
               $this->bbs_count.'.fans_count,'.
                $this->bbs_count.'.articles_count,'.
                $this->bbs_count.'.question_count');
        $this->db->from($this->bbs_fans);
        $this->db->join($this->users_info,$this->users_info.'.user_id='.$this->bbs_fans.'.user_id',left);
        $this->db->join($this->bbs_count,$this->bbs_count.'.user_id='.$this->bbs_fans.'.user_id',left);
        $this->db->join($this->bbs_user,$this->bbs_user.'.user_id='.$this->bbs_fans.'.user_id',left);
        $this->db->where($where);
        $this->db->order_by($this->bbs_fans.".create_time DESC");
        $this->db->limit($limit,$page);
        return $this->db->get()->result_array();
    }
    
    function update_bbs_account($data,$user_id){
        return $this->db->where('user_id',$user_id)->update($this->table, $data);
    }
    //插入打赏记录
    function insert_award($data){
        $this->db->insert($this->bbs_reward, $data);
        if($this->db->affected_rows()){
            return $this->db->insert_id();
        }else{
            return 0;
        }
    }
    
    
    
    function favorite($where,$page,$limit){
        $this->db->select($this->bbs_favorite.'.*,'.$this->bbs_article.'.commentnum,'.$this->bbs_article_category.'.name')->from($this->bbs_favorite);
        $this->db->join($this->bbs_article,$this->bbs_article.'.id='.$this->bbs_favorite.'.article_id',left);
        $this->db->join($this->bbs_article_category,$this->bbs_article.'.cat_id='.$this->bbs_article_category.'.id',left);
        $this->db->where($where);
        $this->db->order_by($this->bbs_favorite.'.create_time','desc');
        $this->db->limit($limit,$page);
        return $this->db->get()->result_array();       
    }
    function reward($where,$page,$limit){
        $this->db->select($this->bbs_reward.'.*,'.$this->bbs_article.'.title,'.$this->bbs_article_category.'.name,'.$this->users_info.'.head_img');
        $this->db->from($this->bbs_reward);
        $this->db->join($this->bbs_article,$this->bbs_article.'.id='.$this->bbs_reward.'.article_id',left);
        $this->db->join($this->bbs_article_category,$this->bbs_article_category.'.id='.$this->bbs_article.'.cat_id',left);
        $this->db->join($this->users_info,$this->users_info.'.user_id='.$this->bbs_reward.'.user_id ',left);
        $this->db->where($where);
        $this->db->order_by($this->bbs_reward.'.create_time','desc');
        $this->db->limit($limit,$page);
        return $this->db->get()->result_array();
    }
    function lettercount($where){
        $this->db->select("*");
        $this->db->from($this->bbs_letter);
        $this->db->where($where);
        return $this->db->get()->num_rows();
    }
    function letter($where,$page,$limit){
        $this->db->select($this->bbs_letter.".*,".$this->users_info.".nickname,".$this->users_info.".head_img");
        $this->db->from($this->bbs_letter);
        $this->db->join($this->users_info,$this->users_info.'.user_id='.$this->bbs_letter.'.addresser_id',left);
        $this->db->where($where);
        $this->db->order_by($this->bbs_letter.'.create_time','desc');
        $this->db->limit($limit,$page);
        return $this->db->get()->result_array();
    }
    function usergroup(){
        $this->db->select("*");
        $this->db->from($this->bbs_user_degree);
        return $this->db->get()->result_array();
    }
    function focus($uid){
        $this->db->select($this->bbs_focus.'.*,'.$this->bbs_article_category.'.name');
        $this->db->from($this->bbs_focus);
        $this->db->join($this->bbs_article_category,$this->bbs_article_category.'.id='.$this->bbs_focus.'.cat_id',left);
        $this->db->where($this->bbs_focus.'.user_id',$uid);
        $this->db->order_by($this->bbs_focus.'.create_time','desc');
        return $this->db->get()->result_array();
    }
    function level($uid){
        $this->db->select($this->bbs_user_degree.'.grouptitle');
        $this->db->from($this->table);
        $this->db->join($this->bbs_user_degree,$this->bbs_user_degree.'.id='.$this->table.'.level',left);
        $this->db->where($this->table.'.user_id',$uid);
        return $this->db->get()->row_array();
    }
    function unfollow($user_id,$followuid){
        $this->db->where('user_id',$user_id);
        $this->db->where('followuid',$followuid);
        $this->db->delete($this->bbs_fans);
        return $this->db->affected_rows();
    }
    
    //累计积分值
    function get_exper_count($where){
        $this->db->select("SUM(experience) exp");
        $this->db->from($this->bbs_exp_list);
        $this->db->where($where);
        $result = $this->db->get()->row_array();
        return $result["exp"];
    }
    
    //插入积分积累
    function insert_exper($data){
        $data['create_time'] = time();
        $data['month'] = date("Ym");
        $data['day'] = date("Ymd");
        $this->db->insert($this->bbs_exp_list, $data);
        if($this->db->affected_rows()){
            return $this->db->insert_id();
        }else{
            return 0;
        }
    }
    //昵称修改
    function update_nickname($nickname,$user_id){
        $this->db->set('nickname',$nickname,TRUE);
        $this->db->where('user_id',$user_id);
        $this->db->update($this->users_info);
        return $this->db->affected_rows();
    }
    //判断系统消息是否发送
    function check_notice($where){
        $this->db->select('*');
        $this->db->from($this->bbs_notice);
        $this->db->where($where);
        return $this->db->get()->row_array();   
    }
    //判断个人动态是否已存在
    function check_behavior($where){
        $this->db->select('*');
        $this->db->from($this->bbs_user_behavior);
        $this->db->where($where);
        return $this->db->get()->row_array();   
    }
    
    //获取用户的粉丝总数
    function get_zhu_count($where){
        return $this->db->select('*')->from($this->bbs_zhu_detail)->where($where)->get()->num_rows();
    }
    
    //获取用户的粉丝
    function get_zhu_list($where,$page,$limit){
        $this->db->select($this->bbs_zhu_detail.'.*');
        $this->db->from($this->bbs_zhu_detail);
        $this->db->where($where);
        $this->db->order_by("id desc");
        $this->db->limit($limit,$page);
        return $this->db->get()->result_array();
    }
}
